package com.yzc.android.event;

import com.yzc.android.ui.base.lifecycle.ILifeCycleComponent;
import com.yzc.android.utils.LogUtils;

/**
 * Created by yzc on 16/4/22.
 */
public class EventLifeCycleHandler implements ILifeCycleComponent {

    private boolean mRegistered = false;

    public EventLifeCycleHandler register() {
        if (!mRegistered) {
            mRegistered = true;
            EventBusManager.getInstance().register(this);
            LogUtils.d("Test", "register");
        }
        return this;
    }

    public synchronized EventLifeCycleHandler tryToUnregister() {
        if (mRegistered) {
            mRegistered = false;
            EventBusManager.getInstance().unregister(this);
            LogUtils.d("Test", "unregister");
        }
        return this;
    }

    public synchronized EventLifeCycleHandler tryToRegisterIfNot() {
        register();
        return this;
    }

    @Override
    public void onBecomesPartiallyInvisible() {

    }

    @Override
    public void onBecomesVisible() {
        register();
    }

    @Override
    public void onBecomesTotallyInvisible() {
        tryToUnregister();
    }

    @Override
    public void onBecomesVisibleFromTotallyInvisible() {

    }

    @Override
    public void onDestroy() {
        tryToUnregister();
    }
}
